package com.school.base.dao.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

import org.hibernate.SQLQuery;
import org.hibernate.criterion.Criterion;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;
import org.hibernate.transform.Transformers;

import com.school.base.dao.ILessonDao;
import com.school.base.model.Lesson;
import com.school.common.base.BaseDao;
import com.school.common.util.MapUtils;

/**
 * @author gaobiao
 * 
 */
public class LessonDao extends BaseDao implements ILessonDao {

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.school.base.dao.ILessonDao#getLessonById(int)
	 */
	@Override
	public Lesson getLessonById(int id) {
		String sql = "select l.*,c.name courseName from b_lesson l join b_course c on c.id=l.course_id where l.id=?";
		List express = new ArrayList();
		express.add(id);
		SQLQuery query = this.getCurrentSession().createSQLQuery(sql);
		query.setResultTransformer(Transformers.ALIAS_TO_ENTITY_MAP);
		if (express != null) {
			for (int i = 0; i < express.size(); i++) {
				query.setParameter(i, express.get(i));
			}
		}
		List list = query.list();
		if (list != null && list.size() > 0) {
			return (Lesson) MapUtils.mapToBean((HashMap<String, Object>) list.get(0), Lesson.class);
		}
		return null;
	}

	private String getExpressSql(Lesson lesson, List express) {
		String sql = "";
		if (lesson != null) {
			if (lesson.getName() != null && !lesson.getName().isEmpty()) {
				sql += " and l.name like ?";
				express.add("%" + lesson.getName() + "%");
			}
			if (lesson.getCourseId() > 0) {
				sql += " and l.course_id=?";
				express.add(lesson.getCourseId());
			}
		}
		return sql;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * com.school.base.dao.ILessonDao#getLessonList(com.school.base.model.Lesson
	 * , int, int)
	 */
	@Override
	public List<Lesson> getLessonList(Lesson lesson, int offset, int pageSize) {
		String sql = "select l.*,c.name lessonName from b_lesson l join b_course c on c.id=l.course_id where 1=1";
		List express = new ArrayList();
		sql += this.getExpressSql(lesson, express);
		sql += " order by l.start_time asc";
		SQLQuery query = this.getCurrentSession().createSQLQuery(sql);
		query.setResultTransformer(Transformers.ALIAS_TO_ENTITY_MAP);
		if (express != null) {
			for (int i = 0; i < express.size(); i++) {
				query.setParameter(i, express.get(i));
			}
		}
		query.setMaxResults(pageSize);
		query.setFirstResult(offset);
		List list = query.list();
		List<Lesson> lessonList = new ArrayList<Lesson>();
		for (Object obj : list) {
			Lesson l = (Lesson) MapUtils.mapToBean((HashMap<String, Object>) obj, Lesson.class);
			lessonList.add(l);
		}
		return lessonList;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * com.school.base.dao.ILessonDao#getTotalCount(com.school.base.model.Lesson
	 * )
	 */
	@Override
	public int getTotalCount(Lesson lesson) {
		String sql = "select count(*) from b_lesson l join b_course c on c.id=l.course_id where 1=1";
		List express = new ArrayList();
		sql += this.getExpressSql(lesson, express);
		return this.getRecordCountBySql(sql, express);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * com.school.base.dao.ILessonDao#updateLesson(com.school.base.model.Lesson)
	 */
	@Override
	public void updateLesson(Lesson lesson) {
		this.update(lesson);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.school.base.dao.ILessonDao#deleteLesson(int)
	 */
	@Override
	public void deleteLesson(int id) {
		Lesson lesson = new Lesson();
		lesson.setId(id);
		this.delete(lesson);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * com.school.base.dao.ILessonDao#insertLesson(com.school.base.model.Lesson)
	 */
	@Override
	public void insertLesson(Lesson lesson) {
		lesson.setCreateTime(new Date());
		this.insert(lesson);
	}

}
